package com.weitu.permission.authorityResource.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.permission.authority.entity.Authority;
import com.weitu.permission.authority.entity.AuthorityResource;
import com.weitu.permission.authority.service.AuthorityService;
import com.weitu.permission.authorityResource.service.AuthorityResourceService;
import com.weitu.util.AjaxResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiMeng 2017/3/22
 */
@Controller
@RequestMapping(value = "permission/authorityResource")
public class AuthorityResourceController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "permission/authorityResource";
    private final AuthorityService authorityService;
    private final AuthorityResourceService authorityResourceService;

    @Autowired
    public AuthorityResourceController(AuthorityService authorityService, AuthorityResourceService authorityResourceService) {
        this.authorityService = authorityService;
        this.authorityResourceService = authorityResourceService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return MODULE_BATH_PATH + "/list";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, String id) {
        Authority authority = authorityService.selectAuthorityById(id);
        model.addAttribute(authority);
        return MODULE_BATH_PATH + "/edit";
    }

    @SystemLog(module = "权限菜单维护", methods = "配置权限菜单")
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public AjaxResult editAjax() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        AjaxResult result = new AjaxResult();

        putParametersIntoMap(paramMap);

        String authorityId = paramMap.get("authorityId").toString();
        String resourcesStr = paramMap.get("resourcesStr").toString();

        if (StringUtils.isEmpty(resourcesStr)){
            if (authorityResourceService.deleteAuthorityResourceByAuthorityId(authorityId)){
                result.setMessage("权限菜单配置成功！");
                result.setSuccess(true);
                return result;
            }
            else {
                result.setMessage("权限菜单配置失败！");
                return result;
            }
        }

        List<AuthorityResource> authorityResourceList = getAuthorityResourceList(authorityId, resourcesStr);
        if (authorityResourceService.updateAuthorityResource(authorityId, authorityResourceList)) {
            result.setMessage("权限菜单配置成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("权限菜单配置失败！");
            return result;
        }
    }

    /**
     * 拼装用于插入的权限菜单列表
     * @param authorityId 权限id
     * @param resourcesStr 菜单字符串，格式: "菜单id,菜单2id"
     * @return 角色权限列表
     */
    private List<AuthorityResource> getAuthorityResourceList(String authorityId, String resourcesStr) {
        List<AuthorityResource> authorityResourceList = new ArrayList<AuthorityResource>();
        String[] resources = resourcesStr.split(",");
        for (String resourceId : resources) {
            AuthorityResource authorityResource = new AuthorityResource();
            authorityResource.setAuthorityId(authorityId);
            authorityResource.setResourceId(resourceId);
            authorityResourceList.add(authorityResource);
        }
        return authorityResourceList;
    }
}
